//
//  ShopInfoTopView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/20.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ShopInfoTopView.h"
#import "ShopInfoEntity.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface ShopInfoTopView ()<SDCycleScrollViewDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) UILabel *shopNameLab;
@property (nonatomic, strong) UIImageView *shopAddressIcon;
@property (nonatomic, strong) UILabel *shopAddressLab;
@property (nonatomic, strong) UIImageView *shopTelIcon;
@property (nonatomic, strong) UILabel *shopTelLab;
@property (nonatomic, strong) UIButton *shopNaviButton;

@property (nonatomic, strong) NSArray<NSString *> *imageUrlStringArray;

@end

@implementation ShopInfoTopView

- (void)setEntity:(ShopInfoEntity *)entity
{
    _entity = entity;
    self.shopNameLab.text = String(entity.orgName);
    self.shopAddressLab.text = String(entity.address);
    self.shopTelLab.text = String(entity.telephone);
}

- (void)setConsumerFileList:(NSArray *)consumerFileList
{
    _consumerFileList = consumerFileList;
    NSMutableArray *temp = @[].mutableCopy;
    for (NSDictionary *dict in consumerFileList) {
        NSString *filePath = dict[@"filePath"];
        [temp addObject:[NSString stringWithFormat:@"%@%@",SERVERADDRS,filePath]];
    }
    self.imageUrlStringArray = temp;
    self.cycleScrollView.imageURLStringsGroup = temp;
}

- (void)naviAction:(id)sender
{
    if (self.naviHandler) {
        self.naviHandler();
    }
}

- (void)setupSubViews
{
    [self addSubview:self.cycleScrollView];
    
    self.shopNameLab = [[UILabel alloc] init];
    self.shopNameLab.font = [UIFont boldSystemFontOfSize:16];
    self.shopNameLab.textColor = kColor333333;
    self.shopNameLab.numberOfLines = 0;
    [self addSubview:self.shopNameLab];
    
    self.shopAddressIcon = [[UIImageView alloc] init];
    self.shopAddressIcon.image = [UIImage imageNamed:@"i_address"];
    [self addSubview:self.shopAddressIcon];
    
    self.shopAddressLab = [[UILabel alloc] init];
    self.shopAddressLab.font = [UIFont systemFontOfSize:14];
    self.shopAddressLab.textColor = kColor666666;
    self.shopAddressLab.numberOfLines = 0;
    [self addSubview:self.shopAddressLab];
    
    self.shopTelIcon = [[UIImageView alloc] init];
    self.shopTelIcon.image = [UIImage imageNamed:@"i_tel"];
    [self addSubview:self.shopTelIcon];
    
    self.shopTelLab = [[UILabel alloc] init];
    self.shopTelLab.font = [UIFont systemFontOfSize:14];
    self.shopTelLab.textColor = kColor666666;
    self.shopTelLab.numberOfLines = 0;
    self.shopTelLab.userInteractionEnabled = YES;
    [self.shopTelLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(telAction)]];
    [self addSubview:self.shopTelLab];
    
    self.shopNaviButton = [[UIButton alloc] init];
    [self.shopNaviButton setTitle:@"导航" forState:UIControlStateNormal];
    [self.shopNaviButton setTitleColor:kColor666666 forState:UIControlStateNormal];
    self.shopNaviButton.titleLabel.font = [UIFont systemFontOfSize:13];
//    self.shopNaviButton.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    self.shopNaviButton.layer.borderColor = kColorBBBBBB.CGColor;
    self.shopNaviButton.layer.borderWidth = 1;
    self.shopNaviButton.layer.cornerRadius = 2;
    self.shopNaviButton.layer.masksToBounds = YES;
    [self addSubview:self.shopNaviButton];
    [self.shopNaviButton addTarget:self action:@selector(naviAction:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.shopNameLab.text = @"丽人医疗美容机构丽人医疗美容机构丽人医疗美容机构丽人医疗美容机构";
//    self.shopAddressLab.text = @"深圳市宝安区新湖路236号二楼丽人医疗美容机构丽人医疗美容机构丽人医疗美容机构";
//    self.shopTelLab.text = @"0755-78990766";
    
}

- (void)setupConstraints
{
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(@(180.0/375*kScreenW));
    }];
    
    [self.shopNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cycleScrollView.mas_bottom).offset(kWidth(22));
        make.left.equalTo(self).offset(kWidth(16));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(16));
    }];
    
    [self.shopAddressIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopNameLab);
        make.top.equalTo(self.shopNameLab.mas_bottom).offset(kWidth(15));
    }];
    
    [self.shopAddressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopAddressIcon.mas_right).offset(kWidth(4));
        make.top.equalTo(self.shopAddressIcon).offset(-kWidth(2));
        make.right.lessThanOrEqualTo(self.shopNaviButton.mas_left).offset(kWidth(-10));
    }];
    
    [self.shopNaviButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-kWidth(16));
        make.centerY.equalTo(self.shopAddressIcon);
        make.width.equalTo(@(kWidth(65)));
        make.height.equalTo(@(kWidth(25)));
    }];
    
    [self.shopTelIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopNameLab);
        make.top.equalTo(self.shopAddressLab.mas_bottom).offset(kWidth(15));
    }];
    
    [self.shopTelLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopTelIcon.mas_right).offset(kWidth(4));
        make.top.equalTo(self.shopTelIcon).offset(-kWidth(2));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(16));
        make.bottom.equalTo(self);
    }];
}

- (void)telAction
{
    if (self.telHandle) {
        self.telHandle();
    }
}

- (void)browserImage:(NSInteger)index
{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentIndex = index;
    [browser show];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    [self browserImage:index];
}

- (SDCycleScrollView *)cycleScrollView
{
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"pic_moren"]];
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    }
    return _cycleScrollView;
}

#pragma mark - YBImageBrowserDataSource
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    return self.imageUrlStringArray.count;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    data.url = [NSURL URLWithString:self.imageUrlStringArray[index]];
    data.sourceObject = self.cycleScrollView;
    return data;
}

@end
